<?php
/**
 * @author hjj
 * 吉客云供应商订单
 * 一般贸易
 */
namespace App\Service\OrderService\OrderRouteService;

use App\Service\Code\Normal;
use App\Model\PssModel\OrderSupplier;
use Service\AppService\IdService;
use App\Service\VendorClient\ShopjackyunApi\ShopJackyunApiClient;
use App\Service\OrderService\BaseSupplierRouterService;

class Shopjackyun extends BaseSupplierRouterService
{
    /**
     * 创建订单
     * @param array $param
     * @return mixed
     */
    public function createOrder( $param = [] ) {
        //下单商品重组
        $supplierGoodsInfo  = $param['supplierGoodsInfo'];
        $supplierCode       = $supplierGoodsInfo['supplierCode'];
        $param['order']['supplier_psn'] = OrderSupplier::instance()->getCreateOrderSn($supplierCode);
        $param['order']['supplier_csn'] = IdService::instance()->getOtherID();
        $param['order']['supplier_dsn'] = IdService::instance()->getOtherID();
        $param['order']['isQueryExpress'] = 1;
        $res = OrderSupplier::instance()->getSupplierCreateOrderData($param);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        $orderItems[] = $res['data'];
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'supplierCode' => $supplierCode,
            'orderItems' => $orderItems
        ]);
    }

    /**
     * 订单支付
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        return $this->callSdkApi(ShopJackyunApiClient::class,$param['apiBody'])->createOrder($param);
    }

    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return $this->callSdkApi(ShopJackyunApiClient::class,$param['apiBody'])->orderCancel($param);
    }

    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale( $param = [] )
    {
        return $this->callSdkApi(ShopJackyunApiClient::class,$param['apiBody'])->afterSale($param);
    }

    /**
     * 获取物流信息
     * @param array $param
     */
    public function queryOrderExpress( $param = [] )
    {
        $queryBody = $param['queryBody'];
        $apiBody   = $param['apiBody'];
        $orderID   = $queryBody['orderID'];
        $orderInfo = OrderSupplier::instance()->info(['id'=>$orderID,'company_id'=>$queryBody['supplierID']]);
        if( empty($orderInfo) ) {
            return Normal::returnCode(Normal::FAIL,"订单信息不存在，请核对！");
        }

        $res = $this->callSdkApi(ShopJackyunApiClient::class,$apiBody)->{__FUNCTION__}($orderInfo);
        if ($res['code'] != Normal::SUC) {
            return Normal::returnCode(Normal::FAIL, $res['msg']);
        }
        if (!isset($res['data']['result']['data']) || empty($res['data']['result']['data'])) {
            return Normal::returnCode(Normal::FAIL, "还未发货");
        }
        if (!isset($res['data']['result']['data']['trades']) || empty($res['data']['result']['data']['trades'])) {
            return Normal::returnCode(Normal::FAIL, "还未发货");
        }

        $expressInfo = reset($res['data']['result']['data']['trades']);
        if (!isset($expressInfo['mainPostid']) || empty($expressInfo['mainPostid'])) {
            return Normal::returnCode(Normal::FAIL, "还未发货");
        }
        if (!isset($expressInfo['logisticName']) || empty($expressInfo['logisticName'])) {
            return Normal::returnCode(Normal::FAIL, "还未发货");
        }
        $expressName = $expressInfo['logisticName'];
        $expressNo   = $expressInfo['mainPostid'];
        return Normal::returnCode(Normal::SUC, "已发货", [
            'expressInfo' => [
                'express_name' => mb_strtoupper($expressName, 'utf-8'),
                'express_sn'   => $expressNo,
            ],
        ]);
    }
}
